package common;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;
import util.HttpUtil;
import util.LogUtil;

public class Notify {
	
	public static final Integer TIME=30;

	public static void assembly(String notifyUrl, JSONObject jsonObject) {
		if (!StringUtils.isEmpty(notifyUrl)) {
			Map<String, String> paramMap = new HashMap<String, String>();
			if (jsonObject.containsKey("version")) {
				paramMap.put("version", jsonObject.getString("version"));
			}
			if (jsonObject.containsKey("no")) {
				paramMap.put("no", jsonObject.getString("no"));
			}
			if (jsonObject.containsKey("order")) {
				paramMap.put("order", jsonObject.getString("order"));
			}
			if (jsonObject.containsKey("rate")) {
				paramMap.put("rate", jsonObject.getString("rate"));
			}
			if (jsonObject.containsKey("cur")) {
				paramMap.put("cur", jsonObject.getString("cur"));
			}
			if (jsonObject.containsKey("amount")) {
				paramMap.put("amount", jsonObject.getString("amount"));
			}
			if (jsonObject.containsKey("issue")) {
				paramMap.put("issue", jsonObject.getString("issue"));
			}
			if (jsonObject.containsKey("signature")) {
				paramMap.put("signature", jsonObject.getString("signature"));
			}
			if (jsonObject.containsKey("code")) {
				paramMap.put("code", jsonObject.getString("code"));
			}
			if (jsonObject.containsKey("message")) {
				paramMap.put("message", jsonObject.getString("message"));
			}
			new Thread(new Runnable() {
				public void run() {
					try {
						new HttpUtil().ssl(notifyUrl, paramMap, TIME*1000);
						StringBuffer send = new StringBuffer();
						for (Map.Entry<String, String> e : paramMap.entrySet()) {
							send.append(e.getKey() + "=" + e.getValue() + "&");
						}
						LogUtil.notify(notifyUrl + "?" + send.toString());
					} catch (Exception e) {
						e.printStackTrace ( );
					}
				}
			}).start();
		}
	}
	
	public static String redirect(String notifyUrl, JSONObject jsonObject) {
		StringBuffer sb=new StringBuffer();
		
		if (!StringUtils.isEmpty(notifyUrl)) {
			sb.append ( notifyUrl );
			if(notifyUrl.indexOf ( "?" )>=0){
				sb.append ( "&" );
			}else{
				sb.append ( "?" );
			}
			sb.append ( "version="+ jsonObject.getString("version")+"&");
			sb.append ( "no="+ jsonObject.getString("no")+"&");
			sb.append ( "order="+ jsonObject.getString("order")+"&");
			sb.append ( "rate="+ jsonObject.getString("rate")+"&");
			sb.append ( "cur="+ jsonObject.getString("cur")+"&");
			sb.append ( "amount="+ jsonObject.getString("amount")+"&");
			sb.append ( "issue="+ jsonObject.getString("issue")+"&");
			sb.append ( "signature="+ jsonObject.getString("signature")+"&");
			sb.append ( "code="+ jsonObject.getString("code")+"&");
			sb.append ( "message="+ jsonObject.getString("message")+"&");
		}
		return sb.toString ( );
	}

	public static void main(String[] args) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("version", "2.0.1");
		jsonObject.put("no", "777700");
		jsonObject.put("order", "761081");
		jsonObject.put("rate", "4");
		jsonObject.put("cur", "5");
		jsonObject.put("amount", "11.00");
		jsonObject.put("issue", "7");
		jsonObject.put("signature", "8");
		jsonObject.put("code", "0000");
		jsonObject.put("message", "10");
		String url = "https://www.moto-pay.com/index.php/Abcdefg3dtwo/return_url";
		try {
			assembly(url, jsonObject);
//			String res=redirect(url, jsonObject);
//			System.out.println ( res ) ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
